import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:jet_rails/jr_app/jr_views/jr_user/jr_user_controller.dart';
import 'package:jet_rails/jr_app/jr_widgets/jr_app_bar.dart';
import 'package:jet_rails/jr_app/jr_widgets/jr_article_widget.dart';
import 'package:jet_rails/jr_app/jr_widgets/jr_empty.dart';
import 'package:jet_rails/jr_app/jr_widgets/jr_scaffold.dart';
import 'package:jet_rails/jr_services/jr_services.dart';

class JrMyArticlesView extends StatefulWidget {
  const JrMyArticlesView({Key? key}) : super(key: key);

  @override
  State<JrMyArticlesView> createState() => _JrMyArticlesViewState();
}

class _JrMyArticlesViewState extends State<JrMyArticlesView> {
  final controller = Get.find<JrUserController>();
  @override
  Widget build(BuildContext context) {
    final myUserId = JrServices.instance.loginUser.value.userId;
    final myArticles =
        JrServices.instance.availableArticle.where((article) => article.author?.userId == myUserId).toList();
    return JrScaffold(
        appBar: JrAppBar(
          title: 'My Articles',
        ),
        body: myArticles.isEmpty
            ? JrEmpty(iconSize: 150.h, text: 'Start your creation')
            : ListView.builder(
                itemCount: myArticles.length,
                padding: EdgeInsets.only(top: 16.h, left: 16.w, right: 16.w),
                itemBuilder: (_, index) {
                  return JrArticleWidget(
                    articleModel: myArticles[index],
                    action: GestureDetector(
                      onTap: () {
                        controller.deleteArticle(myArticles[index]);
                        setState(() {});
                        },
                      child: Image.asset('assets/webp/icon_delete.webp', height: 24.r),
                    ),
                  );
                }));
  }
}
